package top.xcxdl.rural.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import top.xcxdl.rural.config.consts.TimConst;

/**
 * tim 工具类
 *
 * @author haowei
 */
public final class TimUtils {

    /**
     * 创建tim服务 聊天用户
     *
     * @param username 用户名
     * @param nickname 昵称
     * @param avatarUrl 头像
     * @return 是否创建成功
     */
    public static Boolean createTimUser(String username,String nickname,String avatarUrl) {
        // 拼接请求地址
        String url = TimConst.TIM_REQ_URL + "v4/im_open_login_svc/account_import"
                + "?sdkappid=" + TimConst.TIM_APP_ID
                + "&identifier=" + TimConst.ADMIN_USERNAME
                + "&usersig=" + TimConst.ADMIN_USER_SIG
                + "&random=" + RandomUtil.randomInt(100000000, 999999999)
                + "&contenttype=json";
        // 设置请求body
        JSONObject jsonObject = new JSONObject();
        // 用用户名作为唯一id
        jsonObject.put("UserID",username);
        // 昵称
        jsonObject.put("Nick",nickname);
        // 头像
        jsonObject.put("FaceUrl",avatarUrl);
        HttpRequest request = HttpRequest.post(url)
                .body(jsonObject.toJSONString());
        HttpResponse response = request.execute();
        JSONObject responseObj = JSONObject.parseObject(response.body());
        Integer errorCode = responseObj.getInteger("ErrorCode");
        System.out.println(responseObj);
        return errorCode.equals(0);
    }

    /**
     * 发送消息
     * @param fromUsername 来源用户
     * @param toUsername 接收用户
     * @param msgType 消息类型
     * @param msgContent 消息内容
     * @return 是否成功
     */
    public static Boolean sendMsg(String fromUsername,String toUsername,String msgType,String msgContent){
        // 拼接请求地址
        String url = TimConst.TIM_REQ_URL + "v4/openim/sendmsg"
                + "?sdkappid=" + TimConst.TIM_APP_ID
                + "&identifier=" + TimConst.ADMIN_USERNAME
                + "&usersig=" + TimConst.ADMIN_USER_SIG
                + "&random=" + RandomUtil.randomInt(100000000, 999999999)
                + "&contenttype=json";
        // 设置请求body
        JSONObject jsonObject = new JSONObject();
        // 消息同步发送方
        jsonObject.put("SyncOtherMachine",1);
        // 发送人
        jsonObject.put("From_Account",fromUsername);
        // 接收人
        jsonObject.put("To_Account",toUsername);
        // 消息随机数
        jsonObject.put("MsgRandom",RandomUtil.randomInt(100000000, 999999999));
        // 消息类型
        jsonObject.put("MsgType",msgType);
        // 转换msgBody
        JSONObject msgBody = JSONObject.parseObject(msgContent);
        // 消息内容
        jsonObject.put("MsgBody",msgBody);
        HttpRequest request = HttpRequest.post(url)
                .body(jsonObject.toJSONString());
        HttpResponse response = request.execute();
        JSONObject responseObj = JSONObject.parseObject(response.body());
        Integer errorCode = responseObj.getInteger("ErrorCode");
        System.out.println(responseObj);
        return errorCode.equals(0);
    }

    /**
     * 生成usersig
     * @param wxOpenid 原用户名 -- wxOpenid
     * @return usersig
     */
    public static String getUserSig(String wxOpenid){
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TimConst.TIM_APP_ID,TimConst.TIME_APP_KEY);
        // 过期时间为2天
        return tlsSigAPIv2.genUserSig(wxOpenid,60*60*48);
    }
}
